

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>ccqw.degrees &mdash; CCQW 0.1.1 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/sidebar.js"></script>
    <link rel="top" title="CCQW 0.1.1 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for ccqw.degrees</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot; A module to handle nicely conversions between floating point degrees and</span>
<span class="sd">    any combination of degrees, minutes, seconds.</span>

<span class="sd">    :Author: 2009-2011 Roberto Vidmar</span>

<span class="sd">    :Copyright: 2011-2012</span>
<span class="sd">                Nicola Creati &lt;ncreati@inogs.it&gt;</span>
<span class="sd">                Roberto Vidmar &lt;rvidmar@inogs.it&gt;</span>

<span class="sd">    :License: MIT/X11 License (see :download:`license.txt</span>
<span class="sd">                               &lt;../../license.txt&gt;`)</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="k">try</span><span class="p">:</span>
  <span class="kn">from</span> <span class="nn">__init__</span> <span class="kn">import</span> <span class="n">deprecated</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
<div class="viewcode-block" id="deprecated"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.deprecated">[docs]</a>  <span class="k">def</span> <span class="nf">deprecated</span><span class="p">(</span><span class="n">ob</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; The Identity Decorator</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">ob</span>
</div>
<div class="viewcode-block" id="deg2dms"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.deg2dms">[docs]</a><span class="k">def</span> <span class="nf">deg2dms</span><span class="p">(</span><span class="n">degs</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">4</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return degs converted to (signum, degrees, minutes, seconds.decimals)</span>

<span class="sd">    :param degs: degrees</span>
<span class="sd">    :type degs: float</span>
<span class="sd">    :param decimals: number of decimals</span>
<span class="sd">    :type decimals: int</span>
<span class="sd">    :returns: (signum, degrees, minutes, seconds.decimals)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="c"># Convert to DMS.SS</span>
  <span class="n">dmss</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s"> </span><span class="si">%d</span><span class="s"> </span><span class="si">%d</span><span class="s"> </span><span class="si">%.6f</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">degDMSfuzz</span><span class="p">(</span><span class="n">degs</span><span class="p">,</span> <span class="n">decimals</span><span class="p">)</span>
  <span class="p">[</span><span class="n">signum</span><span class="p">,</span> <span class="n">degs</span><span class="p">,</span> <span class="n">mins</span><span class="p">,</span> <span class="n">secs</span><span class="p">]</span> <span class="o">=</span> <span class="n">dmss</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
  <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">signum</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">degs</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">mins</span><span class="p">),</span> <span class="nb">float</span><span class="p">(</span><span class="n">secs</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="deg2dm"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.deg2dm">[docs]</a><span class="k">def</span> <span class="nf">deg2dm</span><span class="p">(</span><span class="n">degs</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">6</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return degs converted to (signum, degrees, minutes.decimals)</span>

<span class="sd">    :param degs: degrees</span>
<span class="sd">    :type degs: float</span>
<span class="sd">    :param decimals: number of decimals</span>
<span class="sd">    :type decimals: int</span>
<span class="sd">    :returns: (signum, degrees, minutes.decimals)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="c"># Convert to DM.MM</span>
  <span class="n">dmm</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s"> </span><span class="si">%d</span><span class="s"> </span><span class="si">%.6f</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">degDMfuzz</span><span class="p">(</span><span class="n">degs</span><span class="p">,</span> <span class="n">decimals</span><span class="p">)</span>
  <span class="p">[</span><span class="n">signum</span><span class="p">,</span> <span class="n">degs</span><span class="p">,</span> <span class="n">mins</span><span class="p">]</span> <span class="o">=</span> <span class="n">dmm</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
  <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">signum</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">degs</span><span class="p">),</span> <span class="nb">float</span><span class="p">(</span><span class="n">mins</span><span class="p">)</span>
</div>
<span class="nd">@deprecated</span>
<div class="viewcode-block" id="point2DMSString"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.point2DMSString">[docs]</a><span class="k">def</span> <span class="nf">point2DMSString</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">decimals</span><span class="p">,</span> <span class="n">separators</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;d&#39;</span><span class="p">,</span> <span class="s">&quot;&#39;&quot;</span><span class="p">,</span> <span class="s">&#39;&quot;&#39;</span><span class="p">]):</span>
  <span class="sd">&quot;&quot;&quot; For a couple of coords in degrees, return the following strings:</span>

<span class="sd">        *[signum] degrees d minutes &#39; seconds.decimals &quot;*</span>

<span class="sd">        *[signum] degrees d minutes &#39; seconds.decimals &quot;*</span>

<span class="sd">    :param coords: (degrees, degrees)</span>
<span class="sd">    :type coords: tuple of float</span>
<span class="sd">    :param decimals: number of decimals for seconds</span>
<span class="sd">    :type decimals: int</span>
<span class="sd">    :param separators: tuple of separators (defaults to [&#39;d&#39;, &quot;&#39;&quot;, &#39;&quot;&#39;])</span>
<span class="sd">    :type separators: tuple of characters</span>
<span class="sd">    :returns: &quot;[signum] degrees d minutes &#39; seconds.decimals &quot; +</span>
<span class="sd">              &quot;[signum] degrees d minutes &#39; seconds.decimals &quot;</span>
<span class="sd">    :rtype: string</span>
<span class="sd">    :raises:</span>

<span class="sd">    .. warning:: This function is deprecated, use d2dms instead</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">format</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;</span><span class="si">%%</span><span class="s">0</span><span class="si">%d</span><span class="s">.</span><span class="si">%d</span><span class="s">f&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">decimals</span><span class="o">+</span><span class="mi">3</span><span class="p">,</span> <span class="n">decimals</span><span class="p">)</span>
  <span class="n">signum</span><span class="p">,</span> <span class="n">degs</span><span class="p">,</span> <span class="n">mins</span><span class="p">,</span> <span class="n">secss</span> <span class="o">=</span> <span class="n">d2dms</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">decimals</span><span class="p">)</span>
  <span class="k">if</span> <span class="n">signum</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
    <span class="n">ssign</span><span class="o">=</span><span class="s">&quot;&quot;</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="n">ssign</span><span class="o">=</span><span class="s">&quot;-&quot;</span>
  <span class="k">return</span> <span class="p">(</span><span class="s">&quot;</span><span class="si">%s%02d%s%02d%s</span><span class="s">&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">ssign</span><span class="p">,</span>
           <span class="n">degs</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">separators</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
           <span class="n">mins</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">separators</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> \
         <span class="p">(</span><span class="n">format</span><span class="p">)</span> <span class="o">%</span> <span class="n">secss</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">separators</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> \
         <span class="p">(</span><span class="s">&quot; </span><span class="si">%s%02d%s%02d%s</span><span class="s">&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">ssign</span><span class="p">,</span>
           <span class="n">degs</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">separators</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
           <span class="n">mins</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">separators</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> \
         <span class="p">(</span><span class="n">format</span><span class="p">)</span> <span class="o">%</span> <span class="n">secss</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">separators</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
</div>
<span class="nd">@deprecated</span>
<div class="viewcode-block" id="point2DMString"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.point2DMString">[docs]</a><span class="k">def</span> <span class="nf">point2DMString</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">decimals</span><span class="p">,</span> <span class="n">separators</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;d&#39;</span><span class="p">,</span> <span class="s">&quot;&#39;&quot;</span><span class="p">]):</span>
  <span class="sd">&quot;&quot;&quot; For a couple of coords in degrees, return the following strings:</span>

<span class="sd">        *[signum] degrees d minutes.decimals &#39;*</span>

<span class="sd">        *[signum] degrees d minutes.decimals &#39;*</span>

<span class="sd">    :param coords: (degrees, degrees)</span>
<span class="sd">    :type coords: tuple of float</span>
<span class="sd">    :param decimals: number of decimals for minutes</span>
<span class="sd">    :type decimals: int</span>
<span class="sd">    :param separators: tuple of separators (defaults to [&#39;d&#39;, &#39;&quot;&#39;])</span>
<span class="sd">    :type separators: tuple of characters</span>
<span class="sd">    :returns: &quot;[signum] degrees d minutes.decimals &#39; +</span>
<span class="sd">              &quot;[signum] degrees d minutes.decimals &#39;</span>
<span class="sd">    :rtype: string</span>
<span class="sd">    :raises:</span>

<span class="sd">    .. warning:: This function is deprecated, use d2dm instead</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">format</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;</span><span class="si">%%</span><span class="s">0</span><span class="si">%d</span><span class="s">.</span><span class="si">%d</span><span class="s">f&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">decimals</span><span class="o">+</span><span class="mi">3</span><span class="p">,</span> <span class="n">decimals</span><span class="p">)</span>
  <span class="n">signum</span><span class="p">,</span> <span class="n">degs</span><span class="p">,</span> <span class="n">minsm</span> <span class="o">=</span> <span class="n">deg2dm</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">decimals</span><span class="p">)</span>
  <span class="k">if</span> <span class="n">signum</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
    <span class="n">ssign</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="n">ssign</span> <span class="o">=</span> <span class="s">&quot;-&quot;</span>
  <span class="k">return</span> <span class="p">(</span><span class="s">&quot;</span><span class="si">%s%02d%s</span><span class="s">&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">ssign</span><span class="p">,</span>
           <span class="n">degs</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">separators</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> \
         <span class="p">(</span><span class="n">format</span><span class="p">)</span> <span class="o">%</span> <span class="n">minsm</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">separators</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span>\
         <span class="p">(</span><span class="s">&quot; </span><span class="si">%s%02d%s</span><span class="s">&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">ssign</span><span class="p">,</span>
           <span class="n">degs</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">separators</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> \
         <span class="p">(</span><span class="n">format</span><span class="p">)</span> <span class="o">%</span> <span class="n">minsm</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">separators</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="num2str"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.num2str">[docs]</a><span class="k">def</span> <span class="nf">num2str</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">6</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return num converted to string with &quot;decimals&quot; decimals</span>

<span class="sd">    :param num: number</span>
<span class="sd">    :type num: float</span>
<span class="sd">    :param decimals: number of decimals</span>
<span class="sd">    :type decimals: int</span>
<span class="sd">    :returns: d.dddddd</span>
<span class="sd">    :rtype: string</span>
<span class="sd">    :raises: StandardError</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">try</span><span class="p">:</span>
    <span class="n">format</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;</span><span class="si">%%</span><span class="s">.</span><span class="si">%d</span><span class="s">f&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="n">decimals</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">format</span><span class="p">)</span> <span class="o">%</span> <span class="n">num</span>
  <span class="k">except</span> <span class="ne">StandardError</span><span class="p">:</span>
    <span class="k">return</span> <span class="s">&quot;&quot;</span>
</div>
<div class="viewcode-block" id="divmod"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.divmod">[docs]</a><span class="k">def</span> <span class="nf">divmod</span><span class="p">(</span><span class="n">dividend</span><span class="p">,</span> <span class="o">*</span><span class="n">divisors</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return quotient and modulos for the integer division of dividend by</span>
<span class="sd">      divisors</span>

<span class="sd">    :param dividend: dividend</span>
<span class="sd">    :type dividend: float</span>
<span class="sd">    :param divisors: variable number of divisors</span>
<span class="sd">    :type divisors: integer(s)</span>
<span class="sd">    :returns: tuple of quotient and modulo(s)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">modulos</span> <span class="o">=</span> <span class="p">()</span>
  <span class="n">q</span> <span class="o">=</span> <span class="n">dividend</span>
  <span class="k">while</span> <span class="n">divisors</span><span class="p">:</span>
    <span class="n">q</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">__divmod__</span><span class="p">(</span><span class="n">divisors</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
    <span class="n">modulos</span> <span class="o">=</span> <span class="p">(</span><span class="n">r</span><span class="p">,)</span> <span class="o">+</span> <span class="n">modulos</span>
    <span class="n">divisors</span> <span class="o">=</span> <span class="n">divisors</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
  <span class="k">return</span> <span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="p">)</span> <span class="o">+</span> <span class="n">modulos</span>
</div>
<div class="viewcode-block" id="secs_to_wdhms"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.secs_to_wdhms">[docs]</a><span class="k">def</span> <span class="nf">secs_to_wdhms</span><span class="p">(</span><span class="n">seconds</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return seconds converted to (week, days, hours, minutes, seconds)</span>

<span class="sd">    :param seconds: number of seconds</span>
<span class="sd">    :type seconds: float</span>
<span class="sd">    :returns: (week, days, hours, minutes, seconds)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">w</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">h</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">s</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">seconds</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mi">60</span><span class="p">,</span> <span class="mi">60</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">(</span><span class="n">w</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">h</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="d2dms"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.d2dms">[docs]</a><span class="k">def</span> <span class="nf">d2dms</span><span class="p">(</span><span class="n">degs</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">4</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Convert degs into degrees, minutes, seconds.decimals</span>

<span class="sd">    :param degs: degrees</span>
<span class="sd">    :type degs: float or numpy array</span>
<span class="sd">    :returns: (degrees, minutes, seconds.decimals)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">try</span><span class="p">:</span>
    <span class="n">fuzz</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.5</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">**</span> <span class="n">decimals</span><span class="p">))</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">degs</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
  <span class="k">except</span><span class="p">:</span>
    <span class="n">fuzz</span> <span class="o">=</span> <span class="mf">0.5</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">**</span> <span class="n">decimals</span><span class="p">)</span>
  <span class="n">absdegs</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">degs</span><span class="p">)</span>
  <span class="n">sign</span> <span class="o">=</span> <span class="n">degs</span> <span class="o">/</span> <span class="n">absdegs</span>
  <span class="n">dint</span><span class="p">,</span> <span class="n">dfrac</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">absdegs</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
  <span class="n">mint</span><span class="p">,</span> <span class="n">mfrac</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">dfrac</span> <span class="o">*</span> <span class="mi">60</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
  <span class="n">secs</span> <span class="o">=</span> <span class="n">mfrac</span> <span class="o">*</span> <span class="mf">60.0</span>
  <span class="k">try</span><span class="p">:</span>
    <span class="n">odds</span> <span class="o">=</span> <span class="p">((</span><span class="n">secs</span> <span class="o">+</span> <span class="n">fuzz</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mf">60.0</span><span class="p">)</span><span class="o">.</span><span class="n">nonzero</span><span class="p">()</span>
    <span class="n">secs</span><span class="p">[</span><span class="n">odds</span><span class="p">]</span> <span class="o">-=</span> <span class="n">fuzz</span><span class="p">[</span><span class="n">odds</span><span class="p">]</span>
  <span class="k">except</span><span class="p">:</span>
    <span class="n">secs</span> <span class="o">-=</span> <span class="n">fuzz</span>
  <span class="k">return</span> <span class="n">sign</span> <span class="o">*</span> <span class="n">dint</span><span class="p">,</span> <span class="n">mint</span><span class="p">,</span> <span class="n">secs</span>
</div>
<div class="viewcode-block" id="d2dm"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.d2dm">[docs]</a><span class="k">def</span> <span class="nf">d2dm</span><span class="p">(</span><span class="n">degs</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">6</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Convert degs into degrees, minutes.decimals</span>

<span class="sd">    :param degs: degrees</span>
<span class="sd">    :type degs: float or numpy array</span>
<span class="sd">    :returns: (degrees, minutes.decimals)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">try</span><span class="p">:</span>
    <span class="n">fuzz</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.5</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">**</span> <span class="n">decimals</span><span class="p">))</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">degs</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
  <span class="k">except</span><span class="p">:</span>
    <span class="n">fuzz</span> <span class="o">=</span> <span class="mf">0.5</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">**</span> <span class="n">decimals</span><span class="p">)</span>
  <span class="n">absdegs</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">degs</span><span class="p">)</span>
  <span class="n">sign</span> <span class="o">=</span> <span class="n">degs</span> <span class="o">/</span> <span class="n">absdegs</span>
  <span class="n">dint</span><span class="p">,</span> <span class="n">dfrac</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">absdegs</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
  <span class="n">mint</span><span class="p">,</span> <span class="n">mfrac</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">dfrac</span> <span class="o">*</span> <span class="mi">60</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
  <span class="n">minutes</span> <span class="o">=</span> <span class="n">mint</span> <span class="o">+</span> <span class="n">mfrac</span>
  <span class="k">try</span><span class="p">:</span>
    <span class="n">odds</span> <span class="o">=</span> <span class="p">((</span><span class="n">minutes</span> <span class="o">+</span> <span class="n">fuzz</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mf">60.0</span><span class="p">)</span><span class="o">.</span><span class="n">nonzero</span><span class="p">()</span>
    <span class="n">minutes</span><span class="p">[</span><span class="n">odds</span><span class="p">]</span> <span class="o">-=</span> <span class="n">fuzz</span><span class="p">[</span><span class="n">odds</span><span class="p">]</span>
  <span class="k">except</span><span class="p">:</span>
    <span class="n">minutes</span> <span class="o">-=</span> <span class="n">fuzz</span>
  <span class="k">return</span> <span class="n">sign</span> <span class="o">*</span> <span class="n">dint</span><span class="p">,</span> <span class="n">minutes</span>
</div>
<span class="k">def</span> <span class="nf">_degDM</span><span class="p">(</span><span class="n">deg</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Degrees to degrees/minutes.</span>

<span class="sd">    :param deg: degrees</span>
<span class="sd">    :type deg: float</span>
<span class="sd">    :returns: (signum, degUnits, minutes)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">if</span> <span class="n">deg</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
    <span class="n">absDeg</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">deg</span><span class="p">)</span>
    <span class="n">signum</span> <span class="o">=</span> <span class="n">deg</span> <span class="o">/</span> <span class="n">absDeg</span>
    <span class="n">degUnits</span><span class="p">,</span> <span class="n">degFraction</span> <span class="o">=</span> <span class="nb">divmod</span> <span class="p">(</span><span class="n">absDeg</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
    <span class="n">minUnits</span><span class="p">,</span> <span class="n">minFraction</span> <span class="o">=</span> <span class="nb">divmod</span> <span class="p">(</span><span class="n">degFraction</span> <span class="o">*</span> <span class="mf">60.0</span><span class="p">,</span> <span class="mf">1.0</span> <span class="p">)</span>
    <span class="n">minutes</span> <span class="o">=</span> <span class="n">minUnits</span> <span class="o">+</span> <span class="n">minFraction</span>
    <span class="c">#return (signum * degUnits, minutes)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">signum</span><span class="p">,</span> <span class="n">degUnits</span><span class="p">,</span> <span class="n">minutes</span><span class="p">)</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="k">return</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">_degDMS</span><span class="p">(</span><span class="n">deg</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Degrees to degrees/minutes/seconds.</span>

<span class="sd">    :param deg: degrees</span>
<span class="sd">    :type deg: float</span>
<span class="sd">    :returns: (signum, degUnits, minUnits, minutes)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">if</span> <span class="n">deg</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
    <span class="n">absDeg</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">deg</span><span class="p">)</span>
    <span class="n">signum</span> <span class="o">=</span> <span class="n">deg</span> <span class="o">/</span> <span class="n">absDeg</span>
    <span class="n">degUnits</span><span class="p">,</span> <span class="n">degFraction</span> <span class="o">=</span> <span class="nb">divmod</span> <span class="p">(</span><span class="n">absDeg</span><span class="p">,</span> <span class="mf">1.0</span> <span class="p">)</span>
    <span class="n">minUnits</span><span class="p">,</span> <span class="n">minFraction</span> <span class="o">=</span> <span class="nb">divmod</span> <span class="p">(</span><span class="n">degFraction</span> <span class="o">*</span> <span class="mf">60.0</span><span class="p">,</span> <span class="mf">1.0</span> <span class="p">)</span>
    <span class="n">seconds</span> <span class="o">=</span> <span class="n">minFraction</span> <span class="o">*</span> <span class="mf">60.0</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">signum</span><span class="p">,</span> <span class="n">degUnits</span><span class="p">,</span> <span class="n">minUnits</span><span class="p">,</span> <span class="n">seconds</span><span class="p">)</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="k">return</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>

<div class="viewcode-block" id="dms2deg"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.dms2deg">[docs]</a><span class="k">def</span> <span class="nf">dms2deg</span> <span class="p">(</span><span class="o">*</span><span class="n">dms</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Degrees / [minutes [ / seconds]] to degrees.</span>

<span class="sd">    :param \*dms: degrees</span>
<span class="sd">    :type \*dms: float(s) or numpy array</span>
<span class="sd">    :returns: degrees</span>
<span class="sd">    :rtype: float</span>
<span class="sd">    :raises:</span>

<span class="sd">    .. warning:: The sign MUST be specified by the first argument.</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">sign</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">dms</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
  <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sign</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
    <span class="n">sign</span><span class="p">[</span><span class="n">sign</span> <span class="o">==</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="k">if</span> <span class="n">sign</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
      <span class="n">sign</span> <span class="o">=</span> <span class="mf">1.</span>

  <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">dms</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
    <span class="k">return</span> <span class="n">sign</span> <span class="o">*</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">dms</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span>
      <span class="p">(</span><span class="n">dms</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="mf">60.0</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">dms</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">/</span> <span class="mf">3600.0</span><span class="p">))</span>
  <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">dms</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
    <span class="k">return</span> <span class="n">sign</span> <span class="o">*</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">dms</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="p">(</span><span class="n">dms</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="mf">60.0</span><span class="p">))</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="k">return</span> <span class="n">dms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="degDMSfuzz"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.degDMSfuzz">[docs]</a><span class="k">def</span> <span class="nf">degDMSfuzz</span><span class="p">(</span><span class="n">deg</span><span class="p">,</span> <span class="n">secDigits</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Convert degrees to deg/min/sec avoiding the *60 seconds* syndrome.</span>

<span class="sd">      If we are displaying integral seconds, then seconds values</span>
<span class="sd">      whose fraction is in the range [0.5, 1.0) will round up,</span>
<span class="sd">      which will make a value like 59.6 display as 60, which is bad.</span>

<span class="sd">      So for that case, we can subtract 0.5 seconds from the degree</span>
<span class="sd">      value before conversion to avoid the problem.  Similarly,</span>
<span class="sd">      if we are displaying seconds with tenths, the trouble range is</span>
<span class="sd">      values with fractions in [0.95, 1.0).  If hundredths, the</span>
<span class="sd">      trouble range is [0.995, 1.0).  In general, for D digits</span>
<span class="sd">      of precision, subtracting (0.5/(10**D)) will prevent the</span>
<span class="sd">      *60 seconds* syndrome.</span>

<span class="sd">      However, we do not want to apply the fuzz unless it actually</span>
<span class="sd">      will cause rounding up.</span>

<span class="sd">    :param deg: degrees</span>
<span class="sd">    :type deg: float</span>
<span class="sd">    :returns: (sign, degrees, minutes, seconds.decimals)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>

  <span class="c">#-- 1 --</span>
  <span class="c"># [ fuzz   :=  0.5 / ( 10 ** secDigits )</span>
  <span class="c">#   d,m,s  :=  deg converted to (deg,min,sec) ]</span>
  <span class="n">fuzz</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.5</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">**</span> <span class="n">secDigits</span><span class="p">))</span>
  <span class="n">sign</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">s</span>  <span class="o">=</span> <span class="n">_degDMS</span><span class="p">(</span><span class="n">deg</span><span class="p">)</span>

  <span class="c">#-- 2 --</span>
  <span class="c"># [ if dms[2] &gt;= (60-fuzz)</span>
  <span class="c">#   else -&gt;</span>
  <span class="c">#     return dms ]</span>
  <span class="k">if</span>  <span class="n">s</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">60</span> <span class="o">-</span> <span class="n">fuzz</span><span class="p">):</span>
    <span class="c">#return (d, m, s - fuzz )</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">sign</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">s</span> <span class="o">-</span> <span class="n">fuzz</span> <span class="p">)</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="c">#return (d, m, s)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">sign</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="degDMfuzz"><a class="viewcode-back" href="../../ccqw.html#ccqw.degrees.degDMfuzz">[docs]</a><span class="k">def</span> <span class="nf">degDMfuzz</span> <span class="p">(</span><span class="n">deg</span><span class="p">,</span> <span class="n">secDigits</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Convert degrees to deg/min avoiding the *60 minutes* syndrome.</span>

<span class="sd">      If we are displaying integral minutes, then minutes values</span>
<span class="sd">      whose fraction is in the range [0.5, 1.0) will round up,</span>
<span class="sd">      which will make a value like 59.6 display as 60, which is bad.</span>
<span class="sd">      So for that case, we can subtract 0.5 minutes from the degree</span>
<span class="sd">      value before conversion to avoid the problem.  Similarly,</span>
<span class="sd">      if we&#39;re displaying minutes with tenths, the trouble range is</span>
<span class="sd">      values with fractions in [0.95, 1.0).  If hundredths, the</span>
<span class="sd">      trouble range is [0.995, 1.0).  In general, for D digits</span>
<span class="sd">      of precision, subtracting (0.5/(10**D)) will prevent the</span>
<span class="sd">      *60 minutes* syndrome.</span>

<span class="sd">      However, we do not want to apply the fuzz unless it actually</span>
<span class="sd">      will cause rounding up.</span>

<span class="sd">    :param deg: degrees</span>
<span class="sd">    :type deg: float</span>
<span class="sd">    :returns: (sign, degrees, minutes.decimals)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>

  <span class="c">#-- 1 --</span>
  <span class="c"># [ fuzz :=  0.5 / ( 10 ** secDigits )</span>
  <span class="c">#   d,m  :=  deg converted to (deg,min) ]</span>
  <span class="n">fuzz</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.5</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">**</span> <span class="n">secDigits</span><span class="p">))</span>
  <span class="n">sign</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">m</span> <span class="o">=</span> <span class="n">_degDM</span><span class="p">(</span><span class="n">deg</span><span class="p">)</span>

  <span class="c">#-- 2 --</span>
  <span class="c"># [ if dm[2] &gt;= (60-fuzz)</span>
  <span class="c">#   else -&gt;</span>
  <span class="c">#     return dm ]</span>
  <span class="k">if</span>  <span class="n">m</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">60</span> <span class="o">-</span> <span class="n">fuzz</span><span class="p">):</span>
    <span class="c">#return (d, m - fuzz)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">sign</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">m</span> <span class="o">-</span> <span class="n">fuzz</span><span class="p">)</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="c">#return (d, m)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">sign</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">m</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/ogs.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Roberto Vidmar, Nicola Creati.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>